package com.toman.ui.operation.enterprise.line;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Interfaces.BaseUrl;
import com.toman.R;
import com.toman.widget.BounceScrollView;
import com.toman.Util.ToastUtil;
import com.toman.toolbar.ToolBarActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
import okhttp3.Response;

public class LineEditActivity extends ToolBarActivity {
    Integer id;
    @BindView(R.id.mWo_Images)
    ImageView mWoImages;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_output)
    EditText etOutput;
    @BindView(R.id.tv_sn_code)
    TextView tvSnCode;
    @BindView(R.id.tv_standard_code)
    TextView tvStandardCode;
    @BindView(R.id.mLinBase)
    LinearLayout mLinBase;
    @BindView(R.id.mCustomScrollView)
    BounceScrollView mCustomScrollView;
    @BindView(R.id.mDeatailsNodata)
    TextView mDeatailsNodata;
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.et_type)
    EditText etType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_edit);
        ButterKnife.bind(this);
        initToolBar("产线编辑", getResources().getColor(R.color.c1));
        initView();
        beltline_monthreport();
    }

    private void initView() {
        id = getIntent().getIntExtra("id", -1);
        etName.setText(getIntent().getStringExtra("name"));

//        etType.setFilters(FilterArray);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_line_edit, menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_type:
                        Intent intent = getIntent();
                        intent.setClass(LineEditActivity.this, LineEditTypeActivity.class);
                        startActivity(intent);
                        break;
                }
                return false;
            }
        });

        return true;
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        if (id == -1) {
            ToastUtil.makeText(this, "数据异常，请重试");
            finish();
        }
        if (etName.length() <= 0) {
            ToastUtil.makeText(this, "请输入产线名称");
            return;
        }
        if (etOutput.length() <= 0) {
            ToastUtil.makeText(this, "请输入产量");
            return;
        }
        editUpdate();
    }

    public void beltline_monthreport() {
        String token = myApplication.getSp().getString("tokenId", "");
        OkHttpUtils
                .post(BaseUrl.beltline_monthreport)
                .params("token", token)
                .params("beltline_id", id.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "  产线月度报表: " + result);
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                JSONObject da = js.getJSONObject("data");

                                JSONObject beltline = da.getJSONObject("beltline");
                                etName.setText(beltline.getString("beltline_name"));
                                etOutput.setText(beltline.getString("target_output"));
                                if (!beltline.getString("product_type").equals("输入产品类型")) {
                                    etType.setText(beltline.getString("product_type"));
                                }

                                JSONObject device = da.getJSONObject("device");
                                tvSnCode.setText(device.getString("device_sn"));
                                tvStandardCode.setText(device.getString("standard_code"));

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /*
      修改产线信息
   */
    public void editUpdate() {
        String token = myApplication.getSp().getString("tokenId", "");
        String name = etName.getText().toString();
        OkHttpUtils
                .post(BaseUrl.beltline_edit)
                .params("token", token)
                .params("id", id.toString())
                .params("beltline_name", name)
                .params("target_output", etOutput.getText().toString())
                .params("product_type", etType.getText().toString())

                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request
                            request, @Nullable Response response) {
                        try {
                            JSONObject js = new JSONObject(s);
                            int code = js.getInt("code");
                            if (code == 1) {
                                Toast.makeText(getApplicationContext(), "修改成功!", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                Toast.makeText(getApplicationContext(), "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
